// pages/dizhi/dizhi.js
const app=getApp();
//获得全局js变量
var util = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgurl:app.data.imgurl,
        applytype:1,//申请类型，1服务中心 2体验店 3运营中心
        applyinfo:[],
        province_: '', 
        provincecode_: '', 
        city_: '', 
        citycode_: '', 
        area_: "", 
        areacode_:'',
        id:0,
        navIndex:1,
        region: ["请选择地址",'',''],
        imgsrc:'../../images/buss.jpg'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getapplyinfo();
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        //获取申请信息
    },
    //点击tab切换
    navTab(e) {
        let id = e.currentTarget.dataset.id
        this.setData({
            navIndex: id,
            id:0,
            applyinfo:[],
            applytype:id,
            imgsrc:'../../images/buss.jpg'
        })
        this.getapplyinfo();
    },
    fileupload() {
        var that=this;
        wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sourceType: ['album', 'camera'],
            maxDuration: 30,
            camera: 'back',
            success(res) {
                var tmppath = res.tempFiles[0].tempFilePath;
                wx.uploadFile({
                    url: app.data.rooturl + 'weixin/fileUpload',
                    header:{token:wx.getStorageSync('token')},
                    name: 'file',
                    filePath: tmppath,
                    formData:{
                    },
                    success:function(res){
                        var data = JSON.parse(res.data);
                        that.setData({
                            imgsrc:data.data
                        })
                    }
                })
            }
        })
    }, 
    getapplyinfo:function(){
        var userid=app.globalData.userId;
        var that = this
        util.httpRrquest(app.data.rooturl + 'user/getUserApply', 'GET', {userid:userid,type:that.data.applytype})
            .then(function (response) {
                that.setData({
                    applyinfo: response.data.data
                });
                var addinfo = response.data.data;
                if(addinfo.hasData == 1){
                    that.setData({
                        id:addinfo.id,
                        province_: addinfo.province, 
                        provincecode_: addinfo.provincecode,
                        city_: addinfo.city, 
                        citycode_: addinfo.citycode,
                        area_: addinfo.area, 
                        areacode_: addinfo.areacode,
                        region: [addinfo.province, addinfo.city, addinfo.area],
                        imgsrc: addinfo.imgSrc
                    });
                }
                
            }, function (error) {
                console.log(error);
            });
    },
    bindRegionChange: function (e) {
        var value = e.detail.value; 
        var code = e.detail.code; 
        var postcode = e.detail.postcode;
        this.setData({
            region: e.detail.value,
            province_: value[0],
            provincecode_: code[0],
            city_: value[1],
            citycode_: code[1],
            area_: value[2],
            areacode_: code[2],
        })
    },
    addapplyInfo:function(){
        var that = this
        if (that.data.province_.length <= 0 || that.data.city_.length <= 0 || that.data.area_.length <= 0) {
            wx.showToast({
                title: '请正确选择地址信息',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        if(that.data.imgsrc=='../../images/buss.jpg'){
            wx.showToast({
                title: '请上传营业执照',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        var userid=app.globalData.userId;
        if (that.data.id > 0) {//////////////////编辑
            var postdata = {
                id: that.data.id,
                province: that.data.province_, provincecode: that.data.provincecode_,
                city: that.data.city_, citycode: that.data.citycode_,
                area: that.data.area_, areacode: that.data.areacode_,
                imgsrc:that.data.imgsrc
            }
            util.httpRrquest(app.data.rooturl + 'user/addApplyInfo', 'POST', postdata)
                .then(function (response) {
                    wx.showToast({
                        title: response.data.msg,
                        icon: 'none',
                        duration: 2000
                    })
                    setTimeout(function () {
                        wx.navigateBack({
                            delta: 1
                        })
                    }, 2000);
                }, function (error) {
                    console.log(error);
                });
        } else {//////////////////////添加
            var postdata = {
                userid:userid,
                type:that.data.applytype,
                province: that.data.province_, provincecode: that.data.provincecode_,
                city: that.data.city_, citycode: that.data.citycode_,
                area: that.data.area_, areacode: that.data.areacode_,
                imgsrc:that.data.imgsrc
            }
            util.httpRrquest(app.data.rooturl + 'user/addApplyInfo', 'POST', postdata)
                .then(function (response) {
                    wx.showToast({
                        title: response.data.msg,
                        icon: 'none',
                        duration: 2000
                    })
                    setTimeout(function () {
                        wx.navigateBack({
                            delta: 1
                        })
                    }, 2000);
                }, function (error) {
                    console.log(error);
                });
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        var shareinfo = app.setShareMessage();
        console.log(shareinfo);
        return {
            title: shareinfo.title,
            path: shareinfo.path,
            imageUrl: shareinfo.imageUrl,
            success: (res) => {
                wx.showToast({
                    title: "分享成功",
                    icon: 'loading'
                })
            }
        }
    }
})